package com.example.javaeekeshe.controller;

import com.example.javaeekeshe.pojo.*;
import com.example.javaeekeshe.service.IAddressService;
import com.example.javaeekeshe.service.IGoodsService;
import com.example.javaeekeshe.service.IOrderService;
import javafx.util.converter.LocalDateStringConverter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpSession;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

@Controller
@RequestMapping("/order")
public class OrderController {
    @Autowired
    IAddressService addressService;
    @Autowired
    IOrderService orderService;
    @Autowired
    IGoodsService goodsService;


    @RequestMapping("/addOrder")
    public String addOrder(OrderBean order, Integer address, HttpSession session ,Model model){
        AccountBean loginUser = (AccountBean) session.getAttribute("loginUser");
        order.setUid(loginUser.getId());
        AddressBean addressBean = addressService.selectAddressByid(address);
        order.setOaddress(addressBean.getAddress());
        order.setOphone(addressBean.getPhone());
        order.setOname(addressBean.getName());
        order.setOtime(LocalDateTime.now());
        order.setOstate(0);
        if (orderService.addOrder(order)==true){
            return "redirect:/order/order/1";
        }else {
            model.addAttribute("msg","部分商品已经无货，下单失败");
            return "redirect:/buy/badcar";
        }

    }

    @RequestMapping("/order/{ye}")
    public String toOrder(@PathVariable("ye") Integer ye, Model model, HttpSession session){
        AccountBean loginUser = (AccountBean) session.getAttribute("loginUser");
        List<FullOrderBean> orders = orderService.getOrderByUid(loginUser.getId(),ye);
        model.addAttribute("orders",orders);
        Integer rows = orderService.rowsUid(loginUser.getId());
        float sum= (float) (rows/5.0);
        if (sum>(int)sum){
            sum++;
        }
        model.addAttribute("sum",(int)sum);
        model.addAttribute("ye",ye);
        return "/buy/order";
    }

    /**
     * 订单管理
     * @param id
     * @param ye
     * @return
     */
    @RequestMapping("/receipt/{id}/{ye}")
    public String receipt(@PathVariable("id") Integer id,@PathVariable Integer ye){
        orderService.receipt(id);
        return "redirect:/order/order/"+ye;
    }

}
